//
//  OperationView.m
//  AAPowerApp
//
//  Created by 李恺然 on 2018/12/24.
//  Copyright © 2018 Karl. All rights reserved.
//

#import "OperationView.h"
#import "UIView+YYAdd.h"
@interface OperationView ()
@property (nonatomic, strong) NSArray *operations;
@property (nonatomic, strong) NSMutableArray *btns;
@end
@implementation OperationView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
        _btns = @[].mutableCopy;
        _operations = @[@"ic_home",@"ic_backward",@"ic_forward",@"ic_refresh",@"ic_logout"];
        [self setupViews];
    }
    return self;
}

- (void)setupViews{
    self.size = CGSizeMake(66, 0);
    for (NSInteger i = 0; i<_operations.count; i++) {
        NSString *operation = _operations[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:operation] forState:UIControlStateNormal];
        [btn setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.6]];
        btn.size = CGSizeMake(44, 44);
        btn.left = 22;
        btn.top = 44 * i;
        [self addSubview:btn];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        [_btns addObject:btn];
        self.height += 44;
    }
   
    
    UIButton *showBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [showBtn setImage:[UIImage imageNamed:@"ic_open"] forState:UIControlStateNormal];
    [showBtn setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.6]];
    showBtn.size = CGSizeMake(22, 44);
    showBtn.left = 0;
    showBtn.centerY = self.height / 2;
    [self addSubview:showBtn];
    [showBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    showBtn.tag = -1;
}

- (void)btnAction:(UIButton *)btn{
    switch (btn.tag) {
        case 0:{
            if (_homepageBlock) {
                _homepageBlock();
            }
        }
            break;
        case 1:{
            if (_goBackBlock) {
                _goBackBlock();
            }
        }
            break;
        case 2:{
            if (_goFrontBlock) {
                _goFrontBlock();
            }
        }
            break;
        case 3:{
            if (_refreshBlock) {
                _refreshBlock();
            }
        }
            break;
        case 4:{
            if (_logoutBlock) {
                _logoutBlock();
            }
        }
            break;
        case -1:{
            if (_showBlock) {
                _showBlock();
            }
        }
            break;
        
            break;
        default:
            break;
    }
}

@end
